package itqf.controller;



import itqf.entity.Address;
import itqf.entity.Cart;
import itqf.entity.Orders;
import itqf.entity.Users;
import itqf.service.AddressServiceImpl;
import itqf.service.CartServiceImpl;
import itqf.service.OrderDetailServiceImpl;
import itqf.service.OrdersServiceImpl;
import itqf.service.impl.AddressService;
import itqf.service.impl.CartService;
import itqf.service.impl.OrderDetailService;
import itqf.service.impl.OrdersService;
import itqf.utils.SysConstant;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

/**
 * ${PACKAGE_NAME}
 * 2020/9/17
 */
@WebServlet("/OrderController")
public class OrderController extends BaseController {
    private CartService cartService = new CartServiceImpl();
    private AddressService addressService = new AddressServiceImpl();
    private OrdersService ordersService = new OrdersServiceImpl();
    private OrderDetailService orderDetailService = new OrderDetailServiceImpl();

    public String getOrderView(HttpServletRequest request, HttpServletResponse response){
        Users user =(Users) request.getSession().getAttribute("user");
        List<Cart> carts = cartService.findAllCart(user.getId());
        List<Address> allAddress = addressService.findAllAddress(user.getId());
        request.setAttribute("carts",carts);
        request.setAttribute("allAddress",allAddress);
        return SysConstant.FORWARD+SysConstant.FALG+"order.jsp";
    }

    public String addOrders(HttpServletRequest request, HttpServletResponse response){
        int aid = Integer.parseInt(request.getParameter("aid"));
        Users user =(Users) request.getSession().getAttribute("user");
        BigDecimal sum = new BigDecimal(request.getParameter("sum"));
        Orders orders = new Orders();
        orders.setAid(aid);
        orders.setUid(user.getId());
        orders.setOccount(sum);
        int account = ordersService.addOrder(orders);
        if(account>1){
            return SysConstant.FORWARD+SysConstant.FALG+"OrderController?method=showAllOrder";
        }else{
            request.setAttribute("msg","订单添加失败");
            return SysConstant.REDIRECT+SysConstant.FALG+"OrderController?method=getOrderView";
        }
    }

    public String showAllOrder(HttpServletRequest request, HttpServletResponse response){
        int aid = Integer.parseInt(request.getParameter("aid"));
        Users user = (Users)request.getSession().getAttribute("user");
        List<Orders> list = ordersService.findAllOrderByUid(user.getId());
        request.setAttribute("orderList",list);
        return SysConstant.FORWARD+SysConstant.FALG+"orderList.jsp";

    }

    public String getOrderDetail(HttpServletRequest request, HttpServletResponse response){
        String oid = request.getParameter("oid");
        Orders orders = orderDetailService.findOrderAndDetail(oid);
        request.setAttribute("orders",orders);
        return SysConstant.FORWARD+SysConstant.FALG+"orderDetail.jsp";

    }

    //修改订单状态
    public  String updateState(HttpServletRequest request, HttpServletResponse resp) throws  Exception{
        String oid = request.getParameter("oid");
        Orders orders  =new Orders();
        orders.setOid(oid);
        orders.setOstate(1);//1

        int i = ordersService.updateOrderState(orders);
        if (i>0){
            request.setAttribute("msg","支付成功");
            return SysConstant.FORWARD+SysConstant.FALG+"message.jsp";

        }else{
            request.setAttribute("msg","订单异常");
            return SysConstant.FORWARD+SysConstant.FALG+"message.jsp";

        }

    }
}
